{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Implementation of Insertion Sort\n",
    "\n",
    "Insertion Sort builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Resources for Review\n",
    "\n",
    "Check out the resources below for a review of Insertion sort!\n",
    "\n",
    "* [Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)\n",
    "* [Visual Algo](http://visualgo.net/sorting.html)\n",
    "* [Animation](http://cs.armstrong.edu/liang/animation/web/InsertionSort.html)\n",
    "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/insertion-sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def insertion_sort(arr):\n",
    "    \n",
    "    # For every index in array\n",
    "    for i in range(1,len(arr)):\n",
    "        \n",
    "        # Set current values and position\n",
    "        currentvalue = arr[i]\n",
    "        position = i\n",
    "        \n",
    "        # Sorted Sublist\n",
    "        while position>0 and arr[position-1]>currentvalue:\n",
    "            \n",
    "            arr[position]=arr[position-1]\n",
    "            position = position-1\n",
    "\n",
    "        arr[position]=currentvalue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 8, 12, 25, 41]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr =[3,5,4,6,8,1,2,12,41,25]\n",
    "insertion_sort(arr)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Good Job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
